<template>
	<view class="content">
		<view class='form'>
			<template v-for="(v,k) in formList">
				<rowChooseVue v-if="v.type == 'picker'" :item="v" v-model="form[v.field]" />
				<rowInputVue v-if="v.type == 'input'" :item="v" v-model="form[v.field]" />
				<rowUploadVue v-if="v.type == 'upload'" :item="v" v-model="form[v.field]" />
				<textareaVue v-if="v.type == 'textarea'" :item="v" v-model="form[v.field]" />
			</template>
		</view>
		<view class="btn" @click="save">提交</view>
	</view>
</template>

<script>
	import rowChooseVue from '@/components/form/rowChoose.vue'
	import rowInputVue from '@/components/form/rowInput.vue'
	import rowUploadVue from '@/components/form/rowUpload.vue'
	import textareaVue from '@/components/form/textarea.vue'
	import {
		addBrand,
		editBrand,
		getGoodsCategory
	} from '@/API/business.js'
	import {
		GetShopDetail
	} from '@/API/store.js'
	export default {
		components: {
			rowChooseVue,
			rowInputVue,
			rowUploadVue,
			textareaVue
		},
		data() {
			return {
				form: {
					ids: 0,
					category_id: '',
					name: '',
					image: '',
					content: ''
				},
				formList: [{
					type: 'picker',
					title: '商品分类',
					field: 'category_id',
					require: true,
					rangeKey: 'name',
					place: '请选择商品分类',
					mode: 'multiSelector',
					list: [],
					level: 3,
					isArrow: true,
					next: 'children'
				}, {
					type: 'input',
					title: '品牌名称',
					field: 'name',
					place: '请输入品牌名称',
					require: true,
					maxlength: 100,
				}, {
					type: 'upload',
					title: '品牌图片',
					field: 'image',
					place: '请上传品牌图片',
					require: true,
				}, {
					type: 'textarea',
					title: '内容介绍',
					field: 'content',
					place: '请输入内容介绍',
					model: 'textarea',
					maxlength: 500,
					require: true,
				}]
			}
		},
		onLoad(options) {
			if (options.data) {
				this.form = JSON.parse(options.data)
				this.form.ids = this.form.id
			}
			if (options.ids) {
				uni.setNavigationBarTitle({
					title: '添加品牌'
				})
				this.form.ids = options.ids
			}
			this.getShopDetail()
		},
		methods: {
			getShow() {
				return this.form.shop_type !== ''
			},
			componentsChanged(e, index) {
				this.formList[index].changed(e)
			},
			getShopDetail() {
				GetShopDetail().then(res => {
					if (res.data.shop_type == 0) {
						this.form.shop_type = 'local'
					} else {
						this.form.shop_type = 'goods'
					}
					this.getCategory()
					// this.getCategory()
				})
			},
			getCategory() {
				this.show = false
				getGoodsCategory({
					type: this.form.shop_type
				}).then(res => {
					this.show = true
					this.formList[0].list = res.data
				})
			},
			save() {
				for (let i of this.formList) {
					if (i.require && !this.form[i.field]) {
						return this.$st.show(i.place)
					}
				}
				if (this.form.ids) {
					editBrand(this.form).then(res => {
						uni.navigateBack()
					})
				} else {
					addBrand(this.form).then(res => {
						uni.navigateBack()
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 20rpx;

		.form {
			padding: 0 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
		}

		.btn {
			position: fixed;
			bottom: 30rpx;
			left: 0;
			right: 0;
			margin: auto;
			width: 610rpx;
			height: 92rpx;
			line-height: 92rpx;
			text-align: center;
			background: linear-gradient(90deg, #DD551B, #F49355);
			border-radius: 46rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 34rpx;
			color: #FFFFFF;
		}
	}
</style>